package a.client.lib_transaction

import a.client.lib_base.ktx.gone
import a.client.lib_base.ktx.toast
import a.client.lib_base.ktx.visible
import a.client.lib_common.constant.RouteUrl
import a.client.lib_common.ui.BaseActivity
import a.client.lib_transaction.databinding.TransactionActivityBinding
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.text.InputType
import androidx.lifecycle.observe
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.transaction_activity.*

/**
@author:jacky
@Date:2021/1/25
 **/
@Route(path = RouteUrl.TranslationActivity)
class TranslationActivity :
    BaseActivity<TransactionActivityBinding, TranslationViewModel>(TranslationViewModel::class.java) {

    override fun initViewBinding() = TransactionActivityBinding.inflate(layoutInflater)

    override fun initView() {
        // 设置EditText
        mBinding.mEdit.run {
            inputType = InputType.TYPE_TEXT_FLAG_MULTI_LINE
            isSingleLine = false
            setHorizontallyScrolling(false)
        }
        // 拷贝
        mBinding.mCopyImg.setOnClickListener {
            val clipboardManager: ClipboardManager =
                getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            clipboardManager.setPrimaryClip(ClipData.newPlainText(mShowTv.text, mShowTv.text))
            toast("已复制")
        }

        // 清空输入框
        mBinding.mCleanImg.setOnClickListener { mBinding.mEdit.setText("") }
        // 翻译按钮监听
        mBinding.mTranslationBtn.setOnClickListener {
            if (mEdit.text.toString().isNotEmpty()) {
                // 请求翻译
                mViewModel.requestTranslation(mEdit.text.toString())
            }
        }
    }

    override fun initViewObserve() {
        // 观察 ViewModel 的 translationLiveData,当其发生改变时,做出响应
        mViewModel.translationLiveData.observe(this) { mBinding.mShowTv.text = it }
        // Loading 图
        mViewModel.isLoading.observe(this) {
            if (it) {
                mBinding.mLoadingLayout.visible()
            } else {
                mBinding.mLoadingLayout.gone()
            }
        }
    }
}